<template>
    <div class="chart-wrap">
       <div class="title">
          <span v-text="name"></span>
          <div class="tools">
            <el-select v-model="query.dateType" placeholder="请选择">
              <el-option
                v-for="item in dateTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-date-picker
              v-model="query.startDate"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
            <el-date-picker
              v-model="query.endDate"
              type="date"
              placeholder="选择日期"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </div>
       </div>
       <div class="chart">
           <div ref="chart" class="content" ></div>
       </div>
    </div>
</template>

<script>
    import mixin from '../mixs/mixin.js';
    import echarts from "echarts";
    export default {
        name: "chart_1" ,
        mixins: [mixin],
        props: {
          name: {
            type: String,
            default: '表名称'
          },
        },
        data() {
          let startDate = new Date(`${new Date().getFullYear()}/1/1`).Format('yyyy-MM-dd');
          let endDate = new Date().Format('yyyy-MM-dd');
          return {
            apiUrl:'newtechanalyse/technologyRegionalDistribution',
            dateTypeList:[
              {label:'月',value:'Month'},
              {label:'天',value:'Day'}
            ],
            query:{
              startDate:startDate, //开始时间
              endDate:endDate, //结束时间
              dateType:'Month', //Month,Day 查询时间类型
              category:'News', //分类 News,Paper,Patent
            },
            xData:[],
            yData:[]
          }
        },
        mounted(){
          this._initChart();
        },
        methods: {
          _initChart() {
             this.$nextTick(()=>{
                 if(!this._vmEchart){
                   this._vmEchart = echarts.init(this.$refs.chart);
                   this._vmEchart.showLoading();
                 }
             })
          },
          asyncDate(){
            //  异步加载数据  通用方法
            if(!this._vmEchart) return;
            this._vmEchart.clear();
            this._vmEchart.showLoading();
            this.api({
              url: this.apiUrl,
              method: "post",
              data:this.$qs.stringify({...this.query,topicId:this.navCurrentId})
            }).then(data => {
               this._formatDate(data)
            });
          },
          getDataURL(){
            // 获取图片base64
            return new Promise((resolve,reject)=>{
              if(!this._vmEchart){reject(); return; }
              resolve(this._vmEchart.getDataURL({
                backgroundColor: this.exportEchartBg
              }))
            });
          },
          _formatDate(data){
            // 根据不同的数据 配合不同的方法
            let xData = [];
            let yData = [];
            data.forEach(item => {
              yData.push(item.sourcename);
              xData.push(item.num);
            });

            this.xData = xData;
            this.yData = yData;
            this._setOption();
          },
          _setOption(){
             let option = {
                 color:['#dd8f04','#6459f0', '#00a3fa'],
                 legend: {
                   orient: 'horizontal',
                   left: 'left',
                   data: ['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']
                 },
                 series : [
                   {
                     name: '访问来源',
                     type: 'pie',
                     radius : '55%',
                     center: ['50%', '60%'],
                     data:[
                       {value:335, name:'直接访问'},
                       {value:310, name:'邮件营销'},
                       {value:234, name:'联盟广告'},
                       {value:135, name:'视频广告'},
                       {value:1548, name:'搜索引擎'}
                     ],
                     itemStyle: {
                       emphasis: {
                         shadowBlur: 10,
                         shadowOffsetX: 0,
                         shadowColor: 'rgba(0, 0, 0, 0.5)'
                       }
                     }
                   }
                 ]
               };

              this._vmEchart.hideLoading();
              this._vmEchart.setOption(option)
          }
        },


    }
</script>

<style lang="scss" scoped>
    .chart{
      position: relative;
      .content{
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
      }
    }

</style>
